package org.kfive.nova.io.nio;

import java.io.*;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/*
* FileCopy.java
* Created  by  lizhanwu
* Created on 2016-5-7 15:16
*/
public class FileCopy {

    public void copyFile(String sPath,String tPath){
        File source = null;
        File target = null;
        FileInputStream in = null;
        FileOutputStream out = null;
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            source = new File(sPath);
            target = new File(tPath);
            in = new FileInputStream(source);
            out = new FileOutputStream(target);
            inChannel = in.getChannel();
            outChannel = out.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            int length = 0;
            while ((length = inChannel.read(buffer))!=-1){
                buffer.flip();
                outChannel.write(buffer);
                buffer.compact();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                inChannel.close();
                outChannel.close();
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
